#!/bin/bash
# 将数据从hive转存到mysql中
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE-$0}")"; pwd)
source ${CUR_DIR}/system_env.sh

XML_FILES_DIR="${CUR_DIR}/runShFiles/$1"
#如果system_env.sh 加载失败，则退出
[ $? -ne 0 ] && exit 1



#运行一个sh文件
runShOneFile(){
	 element=$1
 	 path1=$(getXmlContentValue  $element "path")
     sh1=$(getXmlContentValue  $element "sh")
     args1=$(getXmlContentValue  $element "args")
     path1="${CUR_DIR}/../$path1"
     runShStr="cd ${path1} && bash ${sh1} ${args1}"
     echo "[runSh]"
     echo "${runShStr}"
     echo "_____"
     bash -c "${runShStr}"
     if [ $? -ne 0 ]; then
         echo "=============================================================================================================="
         `ssh root@${mail_server} "python ${mail_script_path}/send_mail.py '生产环境任务执行异常' '${element} ${sh1} ${args1} 执行失败'" > /dev/null 2>&1 &`
     fi
}

#如果没有输入xml文件名称，则全部
#如果输入了xml文件名称，则只运行指定的脚本
oneXmlFileName=$2
for element in `ls ${XML_FILES_DIR}/*.xml`
do  
   if [ -z ${oneXmlFileName} ];then
     echo "$element"
     echo "---------------------"
     runShOneFile $element

   else
      fileName=$(basename $element)
      if [[  ${fileName} =~  ${oneXmlFileName} ]]
		then
		    runShOneFile $element
		     break
		fi
   fi
 done